package org.redmaple.core.template.service.impl;

import java.util.List;
import java.util.Map;

import org.redmaple.core.template.dao.IGenericDao;
import org.redmaple.core.template.dao.IPageDao;
import org.redmaple.core.template.model.BaseModel;
import org.redmaple.core.template.service.IPageService;
import org.redmaple.core.retrieving.RedmapleCriteria;
import org.redmaple.core.retrieving.Sort;
import org.redmaple.core.support.Page;
import org.redmaple.core.support.PageHead;
import org.redmaple.core.support.ServiceInjectionException;

/**
 * 实现分页的一般化Service基类
 * 
 * @author Xiangyun
 *
 * @param <T>
 */
public abstract class PageServiceImpl<T extends BaseModel> extends
		GenericServiceImpl<T> implements IPageService<T> {

	/**
	 * 设置Daoe对象。<br/>
	 * 这个方法要由具体类来实现，格式如下：
	 * 
	 * <br/>@Resource(name = "****")<br/>
	 * public void setDao(IPageDao&lt;T&gt; dao){<br/>
	 * &emsp;this.dao = dao;<br/>
	 * }<br/>
	 * <br/>
	 *                其中****就是你要注入的Dao对象
	 * 
	 * @param service
	 */
	public abstract void setDao(IPageDao<T> pageDao);

	protected final void setDao(IGenericDao<T> genericDao) {
		// 不能将只是IGenericDao，却不是IPageDao的注入到其中
		if (!(genericDao instanceof IPageDao)) {		
			throw new ServiceInjectionException(genericDao.toString()
					+ "不能注入到IPageService实例中。");
		}
		this.dao = genericDao;
	}

	public Page<T> findPageByCondition(Map<String, Object> filterPropertyMap,
			List<Sort> sortValues, PageHead pageInfo) {
		RedmapleCriteria redmapleCriteria = new RedmapleCriteria();
		redmapleCriteria.add(filterPropertyMap, getServicePropertyResolver());
		return ((IPageDao<T>) this.dao).findPageByCondition(redmapleCriteria,
				sortValues, pageInfo);
	}
}
